import Api from '../../constants/Api';

export default {
  namespaced: true,
  state: {
    roleList: [],
    userAllList: [],
    roleDownList: [],
  },
  mutations: {
    roleListTo(state, data) {
      state.roleList = data;
    },
    roleDownListTo(state, data) {
      state.roleDownList = data;
    },
    userListTo(state, data) {
      state.userAllList = data;
    },
  },
  actions: {
    getUserList({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.getUserList,
          data: {
            current: that.subCurrent,
            pageSize: that.subSize,
          },
        })
        .then((res) => {
          commit('userListTo', res.data.records);
          that.subTotal = res.data.total;
          that.subLoading = false;
          that.setUserSelectRow();
        })
        .catch(() => {
          that.subLoading = false;
        });
    },
    getRoleList({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.getRoleList,
          data: {
            current: that.current,
            pageSize: that.pageSize,
            roleName: that.roleName,
          },
        })
        .then((res) => {
          commit('roleListTo', res.data.records);
          that.total = res.data.total;
          that.tableLoading = false;
        })
        .catch(() => {
          that.tableLoading = false;
        });
    },
    getRoleDown({ commit }, that) {
      that
        .$axios({
          method: 'get',
          url: Api.getRoleDown,
        })
        .then((res) => {
          commit('roleDownListTo', res.data);
        });
    },
    addRole({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.addRole,
          data: that.roleForm,
        })
        .then((res) => {
          if (res.code === 20002) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.current = 1;
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    editRole({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.editRole,
          data: that.roleForm,
        })
        .then((res) => {
          if (res.code === 20003) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    removeRole({ state }, that) {
      let url = Api.removeRole.replace('{roleId}', that.roleId);
      return that
        .$axios({
          method: 'delete',
          url: url,
        })
        .then((res) => {
          if (res.code === 20004) {
            that.delVisible = false;
            that.getData();
            that.$message.success(res.msg);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    getUsersInRole({ commit }, that) {
      let url = Api.getUsersInRole.replace('{roleId}', that.roleId);
      return that
        .$axios({
          method: 'get',
          url: url,
        })
        .then((res) => {
          if (res.code === 20001) {
            return res.data;
          } else {
            return [];
          }
        });
    },
    getUserInMenu({ commit }, that) {
      let url = Api.getUserInMenu.replace('{roleId}', that.roleId);
      return that
        .$axios({
          method: 'get',
          url: url,
        })
        .then((res) => {
          if (res.code === 20001) {
            return res.data;
          } else {
            return [];
          }
        });
    },
    bindRoleUsers({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.bindRoleUsers,
          data: that.bindUser,
        })
        .then((res) => {
          if (res.code === 20000) {
            that.userShow = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    bindRoleMenus({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.bindRoleMenus,
          data: {
            roleId: that.bindMenu.roleId,
            menuIds: that.selectMenuIds,
          },
        })
        .then((res) => {
          if (res.code === 20000) {
            that.menuShow = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
  },
};
